package org.dromara.batch;

import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;

import java.util.concurrent.TimeUnit;

// 币价判断任务
public class CoinPriceCheckTasklet implements Tasklet {
    private CoinPriceService coinPriceService;
    private double targetPrice;

    public CoinPriceCheckTasklet(CoinPriceService coinPriceService, double targetPrice) {
        this.coinPriceService = coinPriceService;
        this.targetPrice = targetPrice;
    }

    @Override
    public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
        double currentPrice = coinPriceService.getCurrentPrice();
        System.out.println("Current coin price: " + currentPrice + ", Target price: " + targetPrice);
        if (currentPrice >= targetPrice) {
            return RepeatStatus.FINISHED;
        } else {
            System.out.println("Price condition not met, waiting for 2 minutes to retry...");
            TimeUnit.MINUTES.sleep(2);
            return RepeatStatus.CONTINUABLE;
        }
    }
}


